﻿#include "VAOneXMLAdaptorPhysicalPropertyBeam.h"

#include "Tools/Win64/rapidxml/rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyBeam.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

#include <QDebug>

namespace IO
{
    bool VAOneXMLAdaptorPhysicalPropertyBeam::adaptR()
    {
        // 返回操作是否成功
        return readBeam();
    }

    bool VAOneXMLAdaptorPhysicalPropertyBeam::adaptW()
    {
        return writeBeam();
    }

    bool VAOneXMLAdaptorPhysicalPropertyBeam::readBeam()
    {
        if(!_node)
        {
            return false;
        }
        //转换类型为梁
        Acoustics::FITKPhyscisPropertyBeam* beam = dynamic_cast<Acoustics::FITKPhyscisPropertyBeam*>(_dataObj);
        if(!beam)
        {
            return false;
        }
        bool b = true;
        //获取名称节点
        auto nameNode = _node->first_node("Name");
        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "PhysicalPropertyBeam name is null");
            return false;
        }
        QString strName = readStrNodeValue(nameNode, &b);
        if(b)
        {
            beam->setDataObjectName(strName);//名称可以为空，但是节点不能为空
        }
        double dValue = readDoubleAttribute(_node, "crosssection_area", &b);
        if(b)
        {
            beam->setCrossSectionArea(dValue);
        }
        QString materialValue = readStrAttribute(_node, "material", &b);
        if(b)
        {
            //根据材料名称获取材料id
            int materialID = getMaterialObjectIdByName(materialValue);
            //将id设置到梁中
            beam->setMaterialID(materialID);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"read Material Error" + materialValue);
        }
        double kvValue = readDoubleAttribute(_node, "perimeter", &b);
        if(b)
        {
            beam->setPerimeter(kvValue);
        }
        double pnValue = readDoubleAttribute(_node, "polar_moment", &b);
        if(b)
        {
            beam->setPolarMoment(pnValue);
        }
        double shrValue = readDoubleAttribute(_node, "secondmoment_x", &b);
        if(b)
        {
            beam->setSecondMomentX(shrValue);
        }
        double sosValue = readDoubleAttribute(_node, "secondmoment_y", &b);
        if(b)
        {
            beam->setSecondMomentY(sosValue);
        }
        double shearXValue = readDoubleAttribute(_node, "shear_offset_x", &b);
        if(b)
        {
            beam->setShearOffsetX(shearXValue);
        }

        double shearYValue = readDoubleAttribute(_node, "shear_offset_y", &b);
        if(b)
        {
            beam->setShearOffsetY(shearYValue);
        }
        double torsionalValue = readDoubleAttribute(_node, "torsional_constant", &b);
        if(b)
        {
            beam->setTorsionalConstant(torsionalValue);
        }

        auto noteNode = _node->first_node("Note");
        if (noteNode != nullptr)
        {
            QString strNote = readStrNodeValue(noteNode, &b);
            if(b)
            {
                beam->setDescribe(strNote);
            }
        }

        //获取颜色节点
        auto colorNode = _node->first_node("Color");
        float colorR = 0; float colorG = 0; float colorB = 0;
        //根据节点获取颜色
        readColorNodeValue(colorNode, colorR, colorG, colorB);
        //设置颜色
        beam->setColor(colorR, colorG, colorB);
        return true;
    }

    bool VAOneXMLAdaptorPhysicalPropertyBeam::writeBeam()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKPhyscisPropertyBeam* data = dynamic_cast<Acoustics::FITKPhyscisPropertyBeam*>(_dataObj);

        if(!data)
        {
            return false;
        }

        if(!_node)
        {
            return false;
        }

        //写如属性值
        writeAttribute(_node, "crosssection_area", data->getCrossSectionArea());
        writeAttribute(_node, "perimeter", data->getPerimeter());
        writeAttribute(_node, "secondmoment_x", data->getSecondMomentX());
        writeAttribute(_node, "secondmoment_y", data->getSecondMomentY());
        writeAttribute(_node, "polar_moment", data->getPolarMoment());
        writeAttribute(_node, "torsional_constant", data->getTorsionalConstant());
        writeAttribute(_node, "shear_offset_x", data->getShearOffsetX());
        writeAttribute(_node, "shear_offset_y", data->getShearOffsetY());
        writeAttribute(_node, "material", getMaterialObjectNameById(data->getMaterialID()));

        // 创建名称节点
        createNodeElement(_node, "Name", data->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", data->getDescribe());

        //创建颜色节点
        auto colorNode = createNodeElement(_node, "Color");

        //写入颜色节点信息
        if(colorNode)
        {
            float rgb[3] = {
                static_cast<float>(0.1),
                static_cast<float>(0.3),
                static_cast<float>(0.4) };

            data->getColor(rgb);
            writeAttribute(colorNode, "colorR", rgb[0]);
            writeAttribute(colorNode, "colorG", rgb[1]);
            writeAttribute(colorNode, "colorB", rgb[2]);
        }

        return true;
    }
}
